/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package chat;

import chat.MsgEvent.*;
import java.util.EventObject;
import javax.swing.JTextArea;

/**
 *
 * @author spider391Tang
 */
public class TalkArea extends JTextArea implements EventHandler {

    public void handleEvent(EventObject e) {
        if (e instanceof ShowEvent) {
            if (((ShowEvent) e).type == ShowEvent.NEWLINE) {
                System.out.println((String) e.getSource());
                this.append((String) e.getSource() + "\n");
            } else if (((ShowEvent) e).type == ShowEvent.CLEAR) {
                System.out.println("clear");
                this.setText("");
            }
        }else if(e instanceof MsgPostEvent){
            // Message 101<Student1>: "This is a test".    <-- The server sends "/msg 101 Student1 This is a test".
            this.append("Message " + ((MsgPostEvent)e).getId()+"<"+((MsgPostEvent)e).getAuthor()+">: '" + ((MsgPostEvent)e).getPostMsg()+"'.\n");
        }else if(e instanceof PostEvent){
            // Student1 post a msg '101': "This is a test". 
            this.append(((PostEvent)e).getAuthor() + " post a msg '" + ((PostEvent)e).getId() + "': \""+ ((PostEvent)e).getPostMsg()+"\"\n");
        }

        /// 公子乖 -　顯示最新的訊息，否則每次都要 scroll 到最底部
        this.setCaretPosition(this.getDocument().getLength());
    }
}
